﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mySerialPortHelper.model
{
    public class SerialConnectModel
    {
        public Parity Parity { get; set; }
        public StopBits StopBits { get; set; }

        public string PortName { get; set; }

        public int PortBand { get; set; }

        public string ParityStr
        {
            get
            {
                return Parity.ToString();
            }
            set
            {
                if ("even".Equals(value))
                {
                    Parity = Parity.Even;
                }
                else if ("odd".Equals(value))
                {
                    Parity = Parity.Odd;
                }
                else
                {
                    Parity = Parity.None;
                }
            }
        }

        public int DataBits { get; set; }

        public string StopBitsStr
        {
            get { return ((int)StopBits).ToString(); }
            set
            {

                if ("2".Equals(value))
                {
                    StopBits = StopBits.Two;
                }
                else if ("1.5".Equals(value))
                {
                    StopBits = StopBits.OnePointFive;
                }
                else
                {
                    StopBits = StopBits.One;
                }
            }
        }

        public Boolean Rts { get; set; }

        public Boolean Dtr { get; set; }

        public SerialConnectModel()
        {
            PortBand = 9600;
            ParityStr = "NONE";
            DataBits = 8;
            StopBitsStr = "1";
        }

        public override string ToString()
        {
            return $"portName: {PortName}, portBand: {PortBand}, Parity: {Parity} DataBits: {DataBits}, StopBits: {StopBits}";
        }

    }
}
